from django.db import models
from com.kjssoft.common.globals import DBTableAssist
from com.kjssoft.eagleye.models import EEUser
from com.kjssoft.rainbow.album.models import RainbowPhotoFrameAlbum
from com.kjssoft.rainbow.consts import PhotoFrameConsts
from com.kjssoft.rainbow.image.models import RainbowImage
from com.kjssoft.rainbow.label.models import RainbowPhotoFrameLabel

__author__ = 'Sea'

class RainbowPhotoFrameComment(models.Model):
    comment     = models.CharField(max_length=500)
    commentator = models.ForeignKey(EEUser, related_name='rainbow_photo_frame_comment')
    publish_ms  = models.IntegerField()

    def __unicode__(self):
        return 'rainbow photo frame comment %s (published by user %s)' % (
            self.comment,
            self.commentator.username
        )

    class Meta:
        db_table = DBTableAssist.get_db_table_name('rainbow_photo_frame_comment')

class RainbowPhotoFrameCell(models.Model):
    image     = models.ForeignKey(RainbowImage, related_name='photo_frame_cell_image')
    comment   = models.ForeignKey(RainbowPhotoFrameComment, related_name='photo_frame_cell_comment', null=True, blank=True)
    create_ms = models.IntegerField()

    def __unicode__(self):
        return 'rainbow photo %s(%s)' % (self.id, self.image.uuid)

    class Meta:
        db_table = DBTableAssist.get_db_table_name('rainbow_photo_frame_cell')

class RainbowPhotoFrameManager(models.Manager):
    def get_photos(self, user_instance = None, is_auth_owner = False, album_instance = None):
        if is_auth_owner:
            scope = [
                PhotoFrameConsts.Scope.SELF,
                PhotoFrameConsts.Scope.FRIEND,
                PhotoFrameConsts.Scope.PUBLIC,
            ]
            status = [
                PhotoFrameConsts.Status.DRAFT,
                PhotoFrameConsts.Status.SUBMIT,
                PhotoFrameConsts.Status.VERIFYING,
                PhotoFrameConsts.Status.VERIFIED,
            ]
        else:
            scope = [
                PhotoFrameConsts.Scope.PUBLIC
            ]
            status = [
                PhotoFrameConsts.Status.VERIFIED
            ]
        photos = self.filter(
            status__in = status,
            scope__in = scope
        ).order_by('-publish_ms')
        if user_instance:
            photos = photos.filter(user = user_instance)
        if album_instance:
            photos = photos.filter(album = album_instance)
        return photos

class RainbowPhotoFrame(models.Model):
    user        = models.ForeignKey(EEUser, related_name='photo_frame_user_owner')
    album       = models.ForeignKey(RainbowPhotoFrameAlbum, related_name='photo_frame_alum', null=True, blank=True)
    photos      = models.ManyToManyField(RainbowPhotoFrameCell, related_name='photo_frame_photos')
    comments    = models.ManyToManyField(RainbowPhotoFrameComment, related_name='photo_frame_comments', null=True, blank=True)
    labels      = models.ManyToManyField(RainbowPhotoFrameLabel, related_name='photo_frame_labels', null=True, blank=True)
    md5         = models.CharField(max_length=255, unique=True)
    subject     = models.CharField(max_length=128, null=True, blank=True)
    description = models.CharField(max_length=500, null=True, blank=True)
    publish_ms  = models.IntegerField()
    layout      = models.IntegerField(
        default = PhotoFrameConsts.Layout.SINGLE_PHOTO_PER_FRAME,
        choices = PhotoFrameConsts.Layout.get_choices()
    )
    status      = models.IntegerField(
        default = PhotoFrameConsts.Status.SUBMIT,
        choices = PhotoFrameConsts.Status.get_choices())
    scope       = models.IntegerField(
        default = PhotoFrameConsts.Scope.SELF,
        choices = PhotoFrameConsts.Scope.get_choices()
    )

    def _get_sample_height(self):
        h = 0
        for p in self.photos.all():
            h += 300 * p.image.height / p.image.width
        return h
    sample_height = property(_get_sample_height)

    objects = RainbowPhotoFrameManager()

    def __unicode__(self):
        return 'rainbow photo frame %s of user %s' % (self.id, self.user.username)

    class Meta:
        db_table = DBTableAssist.get_db_table_name('rainbow_photo_frame')
